
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.RC;
using Apps.Models.RC;
using Apps.IBLL.RC;
namespace Apps.BLL.RC
{
	public partial class RC_WEIGHING_IMAGEBLL: Virtual_RC_WEIGHING_IMAGEBLL,IRC_WEIGHING_IMAGEBLL
	{
        

	}
	public class Virtual_RC_WEIGHING_IMAGEBLL
	{
        [Dependency]
        public IRC_WEIGHING_IMAGERepository m_Rep { get; set; }

		public virtual List<RC_WEIGHING_IMAGEModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<RC_WEIGHING_IMAGE> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.WEIGHING_ID.Contains(queryStr)
								
								
								
								
								
								
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<RC_WEIGHING_IMAGEModel> GetPagerList(ref GridPager pager, Expression<Func<RC_WEIGHING_IMAGE, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_IMAGE> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_IMAGEModel> GetList(Expression<Func<RC_WEIGHING_IMAGE, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_IMAGE> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_IMAGEModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<RC_WEIGHING_IMAGEModel>();
		}

        public virtual List<RC_WEIGHING_IMAGEModel> CreateModelList(ref IQueryable<RC_WEIGHING_IMAGE> queryData)
        {

            List<RC_WEIGHING_IMAGEModel> modelList = (from r in queryData
                                              select new RC_WEIGHING_IMAGEModel
                                              {
													WEIGHING_ID = r.WEIGHING_ID,
													WEIGHING_IMAGE1 = r.WEIGHING_IMAGE1,
													WEIGHING_IMAGE2 = r.WEIGHING_IMAGE2,
													WEIGHING_IMAGE3 = r.WEIGHING_IMAGE3,
													WEIGHING_IMAGE4 = r.WEIGHING_IMAGE4,
													WEIGHING_IMAGE5 = r.WEIGHING_IMAGE5,
													WEIGHING_IMAGE6 = r.WEIGHING_IMAGE6,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, RC_WEIGHING_IMAGEModel model)
        {
            try
            {
                RC_WEIGHING_IMAGE entity = m_Rep.GetById(model.WEIGHING_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new RC_WEIGHING_IMAGE();
               				entity.WEIGHING_ID = model.WEIGHING_ID;
				entity.WEIGHING_IMAGE1 = model.WEIGHING_IMAGE1;
				entity.WEIGHING_IMAGE2 = model.WEIGHING_IMAGE2;
				entity.WEIGHING_IMAGE3 = model.WEIGHING_IMAGE3;
				entity.WEIGHING_IMAGE4 = model.WEIGHING_IMAGE4;
				entity.WEIGHING_IMAGE5 = model.WEIGHING_IMAGE5;
				entity.WEIGHING_IMAGE6 = model.WEIGHING_IMAGE6;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<RC_WEIGHING_IMAGE, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.RC_WEIGHING_IMAGE.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, RC_WEIGHING_IMAGEModel model)
        {
            try
            {
                RC_WEIGHING_IMAGE entity = m_Rep.GetById(model.WEIGHING_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.WEIGHING_ID = model.WEIGHING_ID;
				entity.WEIGHING_IMAGE1 = model.WEIGHING_IMAGE1;
				entity.WEIGHING_IMAGE2 = model.WEIGHING_IMAGE2;
				entity.WEIGHING_IMAGE3 = model.WEIGHING_IMAGE3;
				entity.WEIGHING_IMAGE4 = model.WEIGHING_IMAGE4;
				entity.WEIGHING_IMAGE5 = model.WEIGHING_IMAGE5;
				entity.WEIGHING_IMAGE6 = model.WEIGHING_IMAGE6;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual RC_WEIGHING_IMAGEModel GetById(object id)
        {
            if (IsExists(id))
            {
                RC_WEIGHING_IMAGE entity = m_Rep.GetById(id);
                RC_WEIGHING_IMAGEModel model = new RC_WEIGHING_IMAGEModel();
                              				model.WEIGHING_ID = entity.WEIGHING_ID;
				model.WEIGHING_IMAGE1 = entity.WEIGHING_IMAGE1;
				model.WEIGHING_IMAGE2 = entity.WEIGHING_IMAGE2;
				model.WEIGHING_IMAGE3 = entity.WEIGHING_IMAGE3;
				model.WEIGHING_IMAGE4 = entity.WEIGHING_IMAGE4;
				model.WEIGHING_IMAGE5 = entity.WEIGHING_IMAGE5;
				model.WEIGHING_IMAGE6 = entity.WEIGHING_IMAGE6;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual RC_WEIGHING_IMAGEModel GetItem(Expression<Func<RC_WEIGHING_IMAGE, bool>> whereLambda)
        {
 
                RC_WEIGHING_IMAGE entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                RC_WEIGHING_IMAGEModel model = new RC_WEIGHING_IMAGEModel();
                              				model.WEIGHING_ID = entity.WEIGHING_ID;
				model.WEIGHING_IMAGE1 = entity.WEIGHING_IMAGE1;
				model.WEIGHING_IMAGE2 = entity.WEIGHING_IMAGE2;
				model.WEIGHING_IMAGE3 = entity.WEIGHING_IMAGE3;
				model.WEIGHING_IMAGE4 = entity.WEIGHING_IMAGE4;
				model.WEIGHING_IMAGE5 = entity.WEIGHING_IMAGE5;
				model.WEIGHING_IMAGE6 = entity.WEIGHING_IMAGE6;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<RC_WEIGHING_IMAGEModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_ID, "WEIGHING_ID");
				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_IMAGE1, "WEIGHING_IMAGE1");
				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_IMAGE2, "WEIGHING_IMAGE2");
				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_IMAGE3, "WEIGHING_IMAGE3");
				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_IMAGE4, "WEIGHING_IMAGE4");
				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_IMAGE5, "WEIGHING_IMAGE5");
				 excelFile.AddMapping<RC_WEIGHING_IMAGEModel>(x => x.WEIGHING_IMAGE6, "WEIGHING_IMAGE6");
 
            //SheetName
            var excelContent = excelFile.Worksheet<RC_WEIGHING_IMAGEModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new RC_WEIGHING_IMAGEModel();
						 				  entity.WEIGHING_ID = row.WEIGHING_ID;
				  entity.WEIGHING_IMAGE1 = row.WEIGHING_IMAGE1;
				  entity.WEIGHING_IMAGE2 = row.WEIGHING_IMAGE2;
				  entity.WEIGHING_IMAGE3 = row.WEIGHING_IMAGE3;
				  entity.WEIGHING_IMAGE4 = row.WEIGHING_IMAGE4;
				  entity.WEIGHING_IMAGE5 = row.WEIGHING_IMAGE5;
				  entity.WEIGHING_IMAGE6 = row.WEIGHING_IMAGE6;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<RC_WEIGHING_IMAGEModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        RC_WEIGHING_IMAGE entity = new RC_WEIGHING_IMAGE();
                       						entity.WEIGHING_ID = model.WEIGHING_ID;
						entity.WEIGHING_IMAGE1 = model.WEIGHING_IMAGE1;
						entity.WEIGHING_IMAGE2 = model.WEIGHING_IMAGE2;
						entity.WEIGHING_IMAGE3 = model.WEIGHING_IMAGE3;
						entity.WEIGHING_IMAGE4 = model.WEIGHING_IMAGE4;
						entity.WEIGHING_IMAGE5 = model.WEIGHING_IMAGE5;
						entity.WEIGHING_IMAGE6 = model.WEIGHING_IMAGE6;
 
                        db.RC_WEIGHING_IMAGE.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
